package com.demo.helloworld.controller;

import com.demo.helloworld.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {


    private static String prefix = "user";

    @Autowired
    JdbcTemplate jdbcTemplate;

    @ResponseBody
    @GetMapping("/getAll")
    public List getAllUser(){
        return getUserList();
    }

    @GetMapping("/")
    public String user(ModelMap modelMap){
        modelMap.put("userList", getUserList());
        return prefix + "/main";
    }

    private List<User> getUserList(){
        return jdbcTemplate.query("select * from test_user", new BeanPropertyRowMapper<>(User.class));
    }
}
